/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.restsql.db.sql;


import com.restsql.db.ColumnInfo;
import com.restsql.db.Database;
import java.sql.Timestamp;
import java.sql.SQLException;

/**
 * MySQLInputData can be used for parsing incoming data in a way that they can be used in a SQL command.
 * @author dimitris@jmike.gr
 */
public class MySQLInputData {

    public static String parse(Database db, String table, String column, String value) throws InvalidInputDataException, SQLException {
        ColumnInfo columnInfo = db.getColumnInfo(table);
        switch (columnInfo.getDatatype(column)) {
            case BIGINT: {
                return parseBigInt(columnInfo, table, column, value);
            }
            case BINARY: {
                return parseBinary(columnInfo, table, column, value);
            }
            case BIT: {
                return parseBit(columnInfo, table, column, value);
            }
            case BLOB: {
                return parseBlob(columnInfo, table, column, value);
            }
            case CHAR: {
                return parseChar(columnInfo, table, column, value);
            }
            case DATE: {
                return parseDate(columnInfo, table, column, value);
            }
            case DATETIME: {
                return parseDateTime(columnInfo, table, column, value);
            }
            case DECIMAL: {
                return parseDecimal(columnInfo, table, column, value);
            }
            case DOUBLE: {
                return parseDouble(columnInfo, table, column, value);
            }
            case ENUM: {
                return parseEnum(columnInfo, table, column, value);
            }
            case FLOAT: {
                return parseFloat(columnInfo, table, column, value);
            }
            case INT: {
                return parseInt(columnInfo, table, column, value);
            }
            case LONGBLOB: {
                return parseLongBlob(columnInfo, table, column, value);
            }
            case LONGTEXT: {
                return parseLongText(columnInfo, table, column, value);
            }
            case MEDIUMBLOB: {
                return parseMediumBlob(columnInfo, table, column, value);
            }
            case MEDIUMINT: {
                return parseMediumInt(columnInfo, table, column, value);
            }
            case MEDIUMTEXT: {
                return parseMediumText(columnInfo, table, column, value);
            }
            case SET: {
                return parseSet(columnInfo, table, column, value);
            }
            case SMALLINT: {
                return parseSmallInt(columnInfo, table, column, value);
            }
            case TEXT: {
                return parseText(columnInfo, table, column, value);
            }
            case TIME: {
                return parseTime(columnInfo, table, column, value);
            }
            case TIMESTAMP: {
                return parseTimeStamp(columnInfo, table, column, value);
            }
            case TINYBLOB: {
                return parseTinyBlob(columnInfo, table, column, value);
            }
            case TINYINT: {
                return parseTinyInt(columnInfo, table, column, value);
            }
            case TINYTEXT: {
                return parseTinyText(columnInfo, table, column, value);
            }
            case VARBINARY: {
                return parseVarBinary(columnInfo, table, column, value);
            }
            case VARCHAR: {
                return parseVarChar(columnInfo, table, column, value);
            }
            case YEAR: {
                return parseYear(columnInfo, table, column, value);
            }
            default: {
                return value;
            }
        }
    }

    public static String parseBigInt(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseBinary(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseBit(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseBlob(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseChar(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return "'" + value + "'";
    }

    public static String parseDate(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseDateTime(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseDecimal(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseDouble(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseEnum(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return "'" + value + "'";
    }

    public static String parseFloat(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseInt(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseLongBlob(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseLongText(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseMediumBlob(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseMediumInt(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseMediumText(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseSet(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return "'" + value + "'";
    }

    public static String parseSmallInt(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseText(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return "'" + value + "'";
    }

    public static String parseTime(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseTimeStamp(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException, SQLException {
        if (value.replaceAll(" ", "").isEmpty()) {//value is empty
            if (columnInfo.isNullable(column)) {//column excepts NULL values
                return "NULL";
            } else {//column can't be NULL
                throw new InvalidInputDataException(table, column, InputDataErrorEnum.CANNOT_BE_EMPTY);
            }
        } else {//value is not empty
            try {
                Timestamp timestamp = Timestamp.valueOf(value);
                return "'" + timestamp.toString() + "'";
            } catch (IllegalArgumentException ex) {
                throw new InvalidInputDataException(table, column, InputDataErrorEnum.MISFORMATTED);
            }
        }
    }

    public static String parseTinyBlob(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseTinyInt(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseTinyText(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return "'" + value + "'";
    }

    public static String parseVarBinary(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }

    public static String parseVarChar(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return "'" + value + "'";
    }

    public static String parseYear(ColumnInfo columnInfo, String table, String column, String value) throws InvalidInputDataException {
        return value;
    }
}
